<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="20">
        <el-input
          type="textarea"
          autosize="{ minRows: 2, maxRows: 4}"
          placeholder="请输入标题"
          name="title"
          v-model="loginForm.title"
          autocomplete="on">
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button
          @click.native.prevent="handlePostNote"
        >创建帖子</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="20">
        <el-input
            type="textarea"
            autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入标签"
            name="title"
            v-model="loginForm.tagText"
            autocomplete="on">
        </el-input>
      </el-col>
    </el-row>
    <div :gutter="20" style="position: fixed; top: 200px;">
        &nbsp;
    </div>
    <el-row :gutter="20">
      <el-col :span="20">
        <el-input
          type="textarea"
          :autosize="{ minRows: 25, maxRows: 30}"
          placeholder="请输入帖子内容"
          v-model="loginForm.content"
          autocomplete="on">
        </el-input>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  mounted() {
    this.$data.loginForm.signature = this.$store.state.signature
  },
  methods: {
    onSubmit(row) {
      row.title = this.tempData.title;
      row.username = this.tempData.username;
      row.content = this.tempData.content;
      //submit data to back end

      this.$data.handlingRow = row;
      this.dialogVisible = false;
    },
    handlePostNote() {
      var arr = this.loginForm.tagText.split(/[ ]+/)
      this.$data.loginForm.tags = []
      for(var i = 0; i < arr.length; ++i){
        this.$data.loginForm.tags.push(arr[i])
        //alert(arr[i])
        //alert("data "+this.$data.loginForm.tags[i])
      }
      this.loading = true;
      this.$store
        .dispatch("PostNote", this.loginForm)
        .then(response => {
          this.loading = false;
          //let code = response.status;
          if (response.data.status === "success") {
            let saveData = {}
            saveData.pid = response.data.pid;
            saveData.time = response.data.time;
            saveData.username = this.loginForm.username;
            saveData.title = this.loginForm.title;
            saveData.content = this.loginForm.content;
            saveData.dialogVisible = true;
            this.$data.tableData.push(saveData);
          } else {
            this.$router.push({
              path: "/error",
              query: {message: response.data.message}
            });
          }
        })
        .catch(() => {
          this.loading = false;
        });
      this.$router.push({path: "/layout/dashboard"});
      local.reload();
    },
  },
  data() {
    return {
      loginForm: {
        search:"",
        username: "admin",
        password: "123456",
        signature:"",
        title: "",
        content:"",
        pid:"",
        time:"",
        deletepid:"",
        tags: [],
        tagText: "",
        tempData:{
          pid:"",
          title:"",
          username: "",
          content: "",

        },
      }
    }
  }
};
</script>
<style>
</style>
